--
-- Author: shunguo.chen
-- Date: 2019-07-05 16:33:19
--
-- TeamMatchCrossInfo 跨服匹配成功的队伍
--

local UserBaseInfo = require("app.public.dao.UserBaseInfo")

local fileName = string.getModeName(...)
local ClassRef = class(fileName)

function ClassRef:ctor(data)
    if data then
        self:init(data)
    end
end

-- //跨服匹配成功消息通知
-- message S2C_CrossDungeonMatchSuccessNotify {
--     int32 DungeonSubId                          = 1;    //副本ID
--     int32 MatchSuccessId                            = 2;    //用于标识匹配成功队列的ID
--     repeated CrossDungeonReadyInfo Infos    = 3;    //具体成员的准备消息
--     int64 ExpiredStamp                          = 4;    //匹配请求过期时间戳
-- }
function ClassRef:init(data)
    self.targetId = data.DungeonSubId
    self.MatchSuccessId = data.MatchSuccessId
    self.expiredTime = data.ExpiredStamp

    local members = {}
    local myMember
    for i,info in ipairs(data.Infos) do
        local member = {}
        member.baseInfo = UserBaseInfo.new(info.Info)
        member.isReady = info.State
        members[i] = member

        if member.baseInfo.uid == UD:getUid() then
            myMember = member
        end
    end

    -- 1. 真人》机器人 2. uid升序
    table.sort(members, function(l, r)
        local isRobot_l = l.baseInfo:isRobot()
        local isRobot_r = r.baseInfo:isRobot()
        if isRobot_l == isRobot_r then
            return l.baseInfo.uid < r.baseInfo.uid
        else
            return not isRobot_l
        end
    end)

    print("TeamMatchCrossInfo members", #members, myMember)

    self.members = members
    self.myMember = myMember
end

function ClassRef:getReadyNum()
    local readyNum = 0
    for i,member in ipairs(self.members) do
        if member.isReady then
            readyNum = readyNum + 1
        end
    end
    return readyNum, #self.members
end

function ClassRef:getRemainTime()
    local severTime = app:getServerTime()
    return self.expiredTime - math.floor(severTime)
end

function ClassRef:getMembers()
    return self.members
end

function ClassRef:isMySelfReady()
    return self.myMember.isReady
end

function ClassRef:setTeamTargetVo(teamTargetVo)
    self.teamTargetVo = teamTargetVo
end
function ClassRef:getTeamTargetVo()
    return self.teamTargetVo
end

return ClassRef